
package common.packet;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Received by clients when a new client logs in the game server.
 * 
 * @author xissburg
 */
public class ClientIn implements Packet
{
    private ClientInfo clientInfo;

    public ClientIn() {
        clientInfo = new ClientInfo();
    }

    public ClientIn(ClientInfo clientInfo) {
        this.clientInfo = clientInfo.copy();
    }

    public ClientIn(DataInputStream in) throws IOException {
        this();
        read(in);
    }

    public ClientIn(ClientIn clientIn) {
        this(clientIn.getClientInfo());
    }

    public void read(DataInputStream in) throws IOException, IllegalArgumentException
    {
        Packet.Code c = Packet.Code.valueOf(in.readUTF());

        if(!c.equals(Packet.Code.CLIENT_INFO))
            throw new IllegalArgumentException("CLIENT_INFO packet code expected.") ;

        clientInfo.read(in);
    }

    public void write(DataOutputStream out) throws IOException
    {
        out.writeUTF(getCode().name());
        clientInfo.write(out);
    }

    public Code getCode() {
        return Code.CLIENT_IN;
    }

    public ClientIn copy() {
        return new ClientIn(this);
    }

    public ClientInfo getClientInfo() {
        return clientInfo;
    }

    public void setClientInfo(ClientInfo clientInfo) {
        this.clientInfo = clientInfo;
    }
}
